package com.leapp.sk.viewdelegate.adapter.classs
import android.content.Context
import android.text.TextUtils
import android.widget.ImageView
import android.widget.LinearLayout
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.mvp.delegate.BaseAdapterDelegate
import com.example.mvp.view.GlideRoundTransform
import com.leapp.sk.R
import com.leapp.sk.bean.AlbumEntity

/**
 * 班级相册详情视图托管
 * @author  chaohaipeng  on 2018/8/6.
 */
class ClassAlbumDetialImageAdapterDelegate : BaseAdapterDelegate() {
    /**
     * 加载操作
     */
    var mRequestOptions: RequestOptions = RequestOptions()
    override fun getOptionsMenuId(): Int {
        return 0
    }

    override fun initWidget() {
    }

    override fun onDestroy() {
    }

    override fun initLayoutRes(): Int {
         return R.layout.adapter_class_album_detial_image
    }

    /**
     * 初始化数据
     */
    fun initData(context: Context,data: AlbumEntity){
        val iv_acadi_image= getView<ImageView>(R.id.iv_acadi_image)
        val layoutParams: LinearLayout.LayoutParams = iv_acadi_image.layoutParams as LinearLayout.LayoutParams
        layoutParams.width= LKScreenUtil.getScreenWidth()/3- LKScreenUtil.dp2px(2f)
        layoutParams.height= LKScreenUtil.getScreenWidth()/3- LKScreenUtil.dp2px(2f)
        iv_acadi_image.layoutParams=layoutParams
        if (!TextUtils.isEmpty(data.url)){
            setImage(context, data.url, iv_acadi_image)
        }else{
            setImage(context, "", iv_acadi_image)
        }


    }

    /**
     * 设置头像和昵称
     */
    private fun setImage(context: Context, imagePath: String, imageView: ImageView) {
        /**
         * 图片圆角处理
         */
        var mGlideRoundTransform: GlideRoundTransform = GlideRoundTransform.getInstance(context)
        mGlideRoundTransform.setExceptCorner(LKScreenUtil.dp2px(5f),true,true,true,true)
        val tag = imageView.getTag(R.id.tag_image)
        mRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
        mRequestOptions.skipMemoryCache(true)
        mRequestOptions.error(R.mipmap.icon_test_image_album)
        mRequestOptions.transforms(mGlideRoundTransform)
        if ( tag==null||!tag?.equals(imagePath)) {
            val mRequestManager = Glide.with(context)
            mRequestManager?.load(imagePath)
                    ?.apply(mRequestOptions)
                    ?.into(imageView)
            imageView.setTag(R.id.tag_image,imagePath)
        }
    }
}